//
//  VisitorView.swift
//  传智小微博
//
//  Created by lxgoodgo on 16/4/18.
//  Copyright © 2016年 liux.orz. All rights reserved.
//

import SnapKit
import UIKit


//import SnapKit
class VisitorView: UIView {

    //声明一个 闭包
    var vistorClosure:(() -> ())?
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //外界调用方法
    func setVisitorInfo(title title:String?, imgName:String? )
    {
        if title != nil && imgName != nil
        {
           
            //用 可选项用于运算时 一定要解包
            houseImg.image = UIImage(named: imgName!)
            desLabel.text = title
            circleImg.hidden = true
        }
        else
        {
            setAnimation()
        }
    }
    //设置动画
    func setAnimation()
    {
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        ani.duration = 15
        ani.repeatCount = MAXFLOAT
        ani.toValue = 2*M_PI
        ani.removedOnCompletion = false
        
        circleImg.layer.addAnimation(ani, forKey: nil)
    }
    
    //设置ui
    func setupUI()
    {
        self.backgroundColor = UIColor.grayColor()
        //懒加载添加 控件
        addSubview(circleImg)
        addSubview(maskImg)
        addSubview(houseImg)
        addSubview(desLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
//        desLabel.backgroundColor = UIColor.redColor()
        //用snapKit 添加约束
        circleImg.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
            
        }
        
        maskImg.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        houseImg.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self)
        }
        
        desLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(maskImg.snp_bottom).offset(16)
            make.height.equalTo(36)
            make.width.equalTo(236)
            make.centerX.equalTo(maskImg)
        }
        
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(desLabel)

            make.top.equalTo(desLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 36))
            
        }
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(desLabel)
            make.top.equalTo(desLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
    }
    
    @objc private func login()
    {
        //调用闭包
        vistorClosure?()
    }
    
    //懒加载 小圆圈图片
    private lazy var circleImg: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //遮盖图片
    private lazy var maskImg : UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    //小房子
    private lazy var houseImg: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //文字 label
    private lazy var desLabel : UILabel = {
        let label = UILabel()
        label.textColor = UIColor.grayColor()
        label.numberOfLines = 0
        label.textAlignment = NSTextAlignment.Center
        label.font = UIFont.systemFontOfSize(12)
        label.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        return label
    }()
    
    //注册按钮
    private lazy var registerBtn : UIButton =
    {
        let button = UIButton()
        button.backgroundColor = UIColor.whiteColor()
//        button.titleLabel?.text = "注册"
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        button.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        return button
        
    }()
    
    //登录按钮
    private lazy var loginBtn: UIButton =
    {
        let button = UIButton()
        button.backgroundColor = UIColor.whiteColor()
//        button.titleLabel?.text = "登录"
        button.setTitleColor(ThemeColor, forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        return button
        }()
    
    
    
}
